package com.ruoyi.wx.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.emoji.EmojiUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.wx.domain.ACount;
import com.ruoyi.wx.mapper.ACountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.ruoyi.wx.mapper.ADailyMapper;
import com.ruoyi.wx.domain.ADaily;
import com.ruoyi.wx.service.IADailyService;

/**
 * 日常记录Service业务层处理
 * 
 * @author pan
 * @date 2022-11-06
 */
@Service
public class ADailyServiceImpl implements IADailyService 
{
    @Autowired
    private ADailyMapper aDailyMapper;

    @Autowired
    private ACountMapper aCountMapper;

    /**
     * 查询日常记录
     * 
     * @param id 日常记录主键
     * @return 日常记录
     */
    @Override
    public ADaily selectADailyById(Long id)
    {
        // 通过id 查询日常记录
        ADaily aDaily = aDailyMapper.selectADailyById(id);

        // 将转义的别名转为Emoji字符
        String emoji = EmojiUtil.toUnicode(aDaily.getCatText());
        aDaily.setCatText(emoji);

        return aDaily;
    }

    /**
     * 查询日常记录列表
     * 
     * @param aDaily 日常记录
     * @return 日常记录
     */
    @Override
    public List<ADaily> selectADailyList(ADaily aDaily)
    {
        List<ADaily> aDailies = aDailyMapper.selectADailyList(aDaily);

        for (ADaily daily : aDailies){
            // 将转义的别名转为Emoji字符
            String emoji = EmojiUtil.toUnicode(daily.getCatText());
            // 存储
            daily.setCatText(emoji);
        }

        return aDailies;
    }

    /**
     * 新增日常记录
     * 
     * @param aDaily 日常记录
     * @return 结果
     */
    @Override
    public int insertADaily(ADaily aDaily)
    {
        // 设置时间
        aDaily.setCreateTime(DateUtils.getNowDate());

        // 数据中的Emoji字符进行处理
        String alias = EmojiUtil.toAlias(aDaily.getCatText());
        aDaily.setCatText(alias);


        return aDailyMapper.insertADaily(aDaily);
    }

    /**
     * 修改日常记录
     * 
     * @param aDaily 日常记录
     * @return 结果
     */
    @Override
    public int updateADaily(ADaily aDaily)
    {
        try {
            // 当 备注信息==null 与 check == 已通过  则让记录+1
            if(StrUtil.isBlank(aDaily.getRemark()) && aDaily.getCatCheck().equals("已通过")){

                Long id = aDaily.getCatId();  // 获取id

                ACount count = aCountMapper.selectACountById(id);  // 通过id查询出记录

                count.setAllDaily(count.getAllDaily()+1);  // 记录+1

                aCountMapper.updateACount(count);
            }
            aDaily.setRemark("已记录");

            // 数据中的Emoji字符进行处理
            String alias = EmojiUtil.toAlias(aDaily.getCatText());
            aDaily.setCatText(alias);
        }catch (Exception e){
            System.out.println("更新出错！（请检查count表）");
        }

        return aDailyMapper.updateADaily(aDaily);
    }

    /**
     * 批量删除日常记录
     * 
     * @param ids 需要删除的日常记录主键
     * @return 结果
     */
    @Override
    public int deleteADailyByIds(Long[] ids)
    {
        return aDailyMapper.deleteADailyByIds(ids);
    }

    /**
     * 删除日常记录信息
     * 
     * @param id 日常记录主键
     * @return 结果
     */
    @Override
    public int deleteADailyById(Long id)
    {
        return aDailyMapper.deleteADailyById(id);
    }

    /**
     * 分页查询 左边 【通过】
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ArrayList<ADaily> getDailiesList(Integer pageNum, Integer pageSize){
        // 0, 5 ---- 1,5     从0开始取5条【0不包括】  --- 左开右闭
        // 1, 5 ---- 5,5     从5开始取5条【5不包括】

        ArrayList<ADaily> aDailies =aDailyMapper.getDailiesList(pageNum*pageSize, pageSize);

        for (ADaily daily : aDailies){
            // 将转义的别名转为Emoji字符
            String emoji = EmojiUtil.toUnicode(daily.getCatText());
            // 存储
            daily.setCatText(emoji);
        }

        return aDailies;

    }

    /**
     * 分页查询   右边【精选】
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ArrayList<ADaily> getChoiceList(Integer pageNum, Integer pageSize){

        ArrayList<ADaily> aDailies =aDailyMapper.getDailiesList(pageNum*pageSize, pageSize);

        for (ADaily daily : aDailies){
            // 将转义的别名转为Emoji字符
            String emoji = EmojiUtil.toUnicode(daily.getCatText());
            // 存储
            daily.setCatText(emoji);
        }

        return aDailies;
    }

    /**
     * 分页查询 【catId查询】
     * @param pageNum
     * @param pageSize
     * @param catId
     * @return
     */
    @Override
    public ArrayList<ADaily> getCatIdList(Integer pageNum, Integer pageSize, Integer catId){

        ArrayList<ADaily> aDailies = aDailyMapper.getCatIdList(pageNum*pageSize, pageSize, catId);

        for (ADaily daily : aDailies){
            // 将转义的别名转为Emoji字符
            String emoji = EmojiUtil.toUnicode(daily.getCatText());
            // 存储
            daily.setCatText(emoji);
        }

        return aDailies;
    }

    /**
     * 分页查询 【openid查询】
     * @param pageNum
     * @param pageSize
     * @param openid
     * @return
     */
    public ArrayList<ADaily> getUserDaily(Integer pageNum, Integer pageSize, String openid){

        ArrayList<ADaily> aDailies = aDailyMapper.getUserDaily(pageNum*pageSize, pageSize, openid);

        for (ADaily daily : aDailies){
            // 将转义的别名转为Emoji字符
            String emoji = EmojiUtil.toUnicode(daily.getCatText());
            // 存储
            daily.setCatText(emoji);
        }

        return aDailies;

    }


    /**
     * 查询全部未通过的日常
     * @return
     */
    public List<ADaily> getAdminDailies(){
        return aDailyMapper.getAdminDailies();
    }

    /**
     * 查询日常总数
     * @return
     */
    public Long selectADailyCount(){
        return aDailyMapper.selectADailyCount();
    }

    /**
     * 按照时间段——查询日常
     * @param startTime
     * @param endTime
     * @return
     */
    public ArrayList<ADaily> loadSelectTimeDaily(String startTime, String endTime){
        return aDailyMapper.loadSelectTimeDaily(startTime, endTime);
    }
}
